package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallPermission;
import org.linlinjava.litemall.db.domain.LitemallPermissionExample;
import org.linlinjava.litemall.db.mapper.LitemallPermissionDao;
import org.springframework.stereotype.Service;

@Service
public class LitemallPermissionService {
    @Resource
    private LitemallPermissionDao permissionMapper;

    public Set<String> queryByRoleIds(Integer[] roleIds) {
        Set<String> permissions = new HashSet<String>();
        if(roleIds.length == 0){
            return permissions;
        }
        
        LambdaQuery<LitemallPermission>  query = permissionMapper.createLambdaQuery();
        query.andIn(LitemallPermission::getRoleId, Arrays.asList(roleIds));
        query.andEq(LitemallPermission::getDeleted, false);
        List<LitemallPermission> permissionList = query.select(LitemallPermission::getPermission);
        
		/*
		 * LitemallPermissionExample example = new LitemallPermissionExample();
		 * example.or().andRoleIdIn(Arrays.asList(roleIds)).andDeletedEqualTo(false);
		 * 
		 * 
		 * List<LitemallPermission> permissionList =
		 * permissionMapper.selectByExample(example);
		 */
        for(LitemallPermission permission : permissionList){
            permissions.add(permission.getPermission());
        }

        return permissions;
    }


    public Set<String> queryByRoleId(Integer roleId) {
        Set<String> permissions = new HashSet<String>();
        if(roleId == null){
            return permissions;
        }
        
        LambdaQuery<LitemallPermission>  query = permissionMapper.createLambdaQuery();
        query.andEq(LitemallPermission::getRoleId, roleId);
        query.andEq(LitemallPermission::getDeleted, false);
        List<LitemallPermission> permissionList = query.select(LitemallPermission::getPermission);

		/*
		 * LitemallPermissionExample example = new LitemallPermissionExample();
		 * example.or().andRoleIdEqualTo(roleId).andDeletedEqualTo(false);
		 * List<LitemallPermission> permissionList =
		 * permissionMapper.selectByExample(example);
		 */
        for(LitemallPermission permission : permissionList){
            permissions.add(permission.getPermission());
        }

        return permissions;
    }

    public boolean checkSuperPermission(Integer roleId) {
        if(roleId == null){
            return false;
        }
        
        LitemallPermission permission = new LitemallPermission();
        permission.setRoleId(roleId);
        permission.setPermission("*");
        permission.setDeleted(false);
        
        return permissionMapper.templateCount(permission)!=0;
		/*
		 * LitemallPermissionExample example = new LitemallPermissionExample();
		 * example.or().andRoleIdEqualTo(roleId).andPermissionEqualTo("*").
		 * andDeletedEqualTo(false); return permissionMapper.countByExample(example) !=
		 * 0;
		 */
    }

    public void deleteByRoleId(Integer roleId) {
    	
    	LambdaQuery<LitemallPermission> query = permissionMapper.createLambdaQuery();
    	
    	query.andEq(LitemallPermission::getRoleId, roleId);
    	query.andEq(LitemallPermission::getDeleted, false);
    	
    	LitemallPermission permission = new LitemallPermission();
        permission.setDeleted(true);
        query.updateSelective(permission);
    	
		/*
		 * LitemallPermissionExample example = new LitemallPermissionExample();
		 * example.or().andRoleIdEqualTo(roleId).andDeletedEqualTo(false);
		 * permissionMapper.logicalDeleteByExample(example);
		 */
    }

    public void add(LitemallPermission litemallPermission) {
        litemallPermission.setAddTime(LocalDateTime.now());
        litemallPermission.setUpdateTime(LocalDateTime.now());
        permissionMapper.insertTemplate(litemallPermission,true);
        //permissionMapper.insertSelective(litemallPermission);
    }
}
